//Documentation complete
/**
 * @author Kazu
 * @version 2009AP06
 */

package problemDomain;

import static problemDomain.ErrorBean.*;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.encodeEntities;

/**
 * Represents the delivery of an order.
 */
public class DeliveryBean implements Bean {	
	
	/* Constants */
	
	private static final long serialVersionUID = -335003814373354318L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	private int id;
	private String firstname;
	private String lastname;
	private String phone;
	private String address;
	private String city;
	private String province;
	private String postalCode;
	private String dwellingType;
	private String roomNo;			//for when the type of dwelling is apartment, hotel
	private String buildingName;	//for when the type of dwelling is hotel or company building
	private String deliveryInstruction;
	private String deliveryStatus;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>DeliveryBean</code> with default values. 
	 */
	public DeliveryBean () {
		this.id = 0;
		this.address = "";
		this.city = "";
		this.province = "";
		this.postalCode = "";
		this.firstname = "";
		this.lastname = "";
		this.phone = "";
		this.dwellingType = "";//null;
		this.roomNo = "";
		this.buildingName = "";
		this.deliveryInstruction = "";
		this.deliveryStatus = "";
	}
	
	/**
	 * Constructs a <code>DelveryBean</code> with an ID number.
	 * @param id the ID number of the delivery
	 */
	public DeliveryBean (int id) {
		this();
		this.id = id;
	}
	
	/**
	 * Constructs a <code>DeliveryBean</code> with specified values.
	 * @param id ID number of the delivery
	 * @param firstname First name of the person who receives the delivery
	 * @param lastname Last name of the person who receives the delivery
	 * @param phone Phone number of the person who receives the delivery
	 * @param address Address of the delivery
	 * @param city City of the delivery address
	 * @param province Province of the delivery address
	 * @param postalCode Postal code of the delivery address
	 * @param dwellingType Type of the dwelling that the delivery is destined to
	 * @param buildingName Building name where the delivery is destined to
	 * @param roomNo Room number where the delivery is destined to
	 * @param deliveryStatus the status of delivery
	 * @param deliveryInstruction Instruction regarding to how to get to the destination
	 */
	public DeliveryBean (int id, String firstname, String lastname,
			String phone, String address, String city, String province, String postalCode, 
			String dwellingType, String buildingName, String roomNo, 
			String deliveryStatus, String deliveryInstruction) {
		this.buildingName = buildingName;
		this.city = city;
		this.province = province;
		this.id = id;
		this.dwellingType = dwellingType;
		this.postalCode = postalCode;
		this.firstname = firstname;
		this.lastname = lastname;
		this.phone = phone;
		this.roomNo = roomNo;
		this.address = address;
		this.deliveryInstruction = deliveryInstruction;
		this.deliveryStatus = deliveryStatus;
	}
	
	/* END Constructors */
	
	
	/* Accessors */

	/**
	 * Returns the ID number of the delivery.
	 * @return the ID number of the delivery 
	 */
	public int getId () {
		return id;
	}
	
	/**
	 * Returns the address of the delivery.
	 * @return the address of the delivery
	 */
	public String getAddress () {
		return address;
	}
	
	/**
	 * Returns the city of the delivery address.
	 * @return the city of the delivery address 
	 */
	public String getCity () {
		return city;
	}
	
	/**
	 * Returns the province of the delivery address.
	 * @return the province of the delivery address
	 */
	public String getProvince () {
		return province;
	}
	
	/**
	 * Returns the postal code of the delivery address.
	 * @return the postal code of the delivery address
	 */
	public String getPostalCode () {
		return postalCode;
	}
	
	/**
	 * Returns the first name on the delivery.
	 * @return the first name on the delivery
	 */
	public String getFirstname () {
		return firstname;
	}

	/**
	 * Returns the last name on the delivery.
	 * @return the last name on the delivery
	 */
	public String getLastname () {
		return lastname;
	}

	/**
	 * Returns the phone number on the delivery
	 * @return the phone number on the delivery
	 */
	public String getPhone () {
		return phone;
	}

	/**
	 * Returns the type of the dwelling that the delivery is destined to.
	 * @return the type of the dwelling that the delivery is destined to
	 */
	public String getDwellingType () {
		return dwellingType;
	}
	
	/**
	 * Returns the room number where the delivery is destined to.
	 * @return the room number where the delivery is destined to
	 */
	public String getRoomNo () {
		return roomNo;
	}
	
	/**
	 * Returns the building name where the delivery is destined to.
	 * @return the building name where the delivery is destined to
	 */
	public String getBuildingName () {
		return buildingName;
	}
	
	/**
	 * Returns the delivery instructions.
	 * @return the delivery instructions
	 */
	public String getDeliveryInstruction () {
		return deliveryInstruction;
	}
	
	/**
	 * Returns the status of delivery.
	 * @return the status of delivery
	 */
	public String getDeliveryStatus () {
		return deliveryStatus;
	}
	
	/* END Accessors */
	
	
	/* Mutators */

	/**
	 * Sets the ID number of the delivery.
	 * @param id the ID of the delivery
	 */
	public void setId (int id) {
		this.id = id;
	}
	
	/**
	 * Sets the address of the delivery.
	 * @param address the street address of the delivery address
	 */
	public void setAddress (String address) {
		this.address = address;
	}
	
	/**
	 * Sets the city of the delivery address.
	 * @param city the city of the delivery address
	 */
	public void setCity (String city) {
		this.city = city;
	}
	
	/**
	 * Sets the province of the delivery address.
	 * @param province the province of the delivery address
	 */
	public void setProvince (String province) {
		this.province = province;
	}
	
	/**
	 * Sets the postal code of the delivery address.
	 * @param postalCode the postal code of the delivery address
	 */
	public void setPostalCode (String postalCode) {
		this.postalCode = postalCode;
	}
	
	/**
	 * Sets the first name on the delivery.
	 * @param firstname the first name on the delivery
	 */
	public void setFirstname (String firstname) {
		this.firstname = firstname;
	}
	
	/**
	 * Sets the last name on the delivery.
	 * @param lastname the last name on the delivery
	 */
	public void setLastname (String lastname) {
		this.lastname = lastname;
	}
	
	/**
	 * Sets the phone number on the delivery
	 * @param phone the phone number on the delivery
	 */
	public void setPhone (String phone) {
		this.phone = phone;
	}
	
	/**
	 * Sets the dwelling type of the delivery destination.
	 * @param dwellingType the dwelling type of the delivery destination
	 */
	public void setDwellingType (String dwellingType) {
		this.dwellingType = dwellingType;
	}
	
	/**
	 * Sets the room number of the delivery destination.
	 * @param roomNo the room number of the delivery destination
	 */
	public void setRoomNo (String roomNo) {
		this.roomNo = roomNo;
	}
	
	/**
	 * Sets the building name of the delivery destination.
	 * @param buildingName the building name of the delivery destination
	 */
	public void setBuildingName (String buildingName) {
		this.buildingName = buildingName;
	}
	
	/**
	 * Sets the instruction regarding to how to get to the destination.
	 * @param deliveryInstruction the instructions for the delivery
	 */
	public void setDeliveryInstruction (String deliveryInstruction) {
		this.deliveryInstruction = deliveryInstruction;
	}

	/**
	 * Sets the status of the delivery.
	 * @param deliveryStatus the status of the delivery
	 */
	public void setDeliveryStatus (String deliveryStatus) {
		this.deliveryStatus = deliveryStatus;
	}
	
	/* END Mutators */
	
	
	/* (Bean) Methods */
	
	@Override
	public boolean isValid () throws ErrorBean {
		if (id < 0)
			throwError(
				DELIVERY_ID_INVALID,
				String.valueOf(id),
				this);
		
		if (firstname != null && firstname.length() > 30)
			throwError(
				DELIVERY_FIRSTNAME_TOO_LONG,
				firstname,
				this);
		
		if (lastname != null && lastname.length() > 30)
			throwError(
				DELIVERY_LASTNAME_TOO_LONG,
				lastname,
				this);
		
		if (address != null && address.length() > 40)
			throwError(
				DELIVERY_ADDRESS_TOO_LONG,
				address,
				this);
		
		if (city!= null && city.length() > 30)
			throwError(
				DELIVERY_CITY_TOO_LONG,
				city,
				this);
		
		if (province != null && province.length() > 2)
			throwError(
				DELIVERY_PROVINCE_TOO_LONG,
				province,
				this);
		
		if (postalCode != null && !postalCode.toUpperCase().matches("[ABCEGHJ-NPRSTVXZ]\\d[A-Z]\\d[A-Z]\\d"))
			throwError(
				DELIVERY_POSTAL_CODE_INVALID,
				postalCode,
				this);
		
		return true;
	}
	
	@Override
	public String toXML () {
		StringBuilder sb = new StringBuilder();
		sb.append("<delivery>");
		sb.append("<id>" + id + "</id>");
		sb.append("<address>" + address + "</address>");
		sb.append("<city>" + city + "</city>");
		sb.append("<province>" + province + "</province>");
		sb.append("<postal>" + postalCode + "</postal>");
		sb.append("<firstname>" + firstname+ "</firstname>");
		sb.append("<lastname>" + lastname+ "</lastname>");
		sb.append("<phone>" + phone + "</phone>");
		sb.append("<dwelling>" + (dwellingType != null ? dwellingType.toString() : "") + "</dwelling>");
		sb.append("<room>" + roomNo + "</room>");
		sb.append("<building>" + buildingName + "</building>");
		sb.append("<instruction>" + deliveryInstruction + "</instruction>");
		sb.append("<status>" + deliveryStatus + "</status>");
		sb.append("</delivery>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Methods */
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("ID: " + id + '\n');
		sb.append("Address: " + address + '\n');
		sb.append("City: " + city + '\n');
		sb.append("Province: " + province + '\n');
		sb.append("Postal Code:" + postalCode + '\n');
		sb.append("First Name: " + firstname+ "\n");
		sb.append("Last Name: " + lastname+ "\n");
		sb.append("Phone: " + firstname+ "\n");
		sb.append("Dwelling Type: " + dwellingType + '\n');
		sb.append("Room No.: " + roomNo + '\n');
		sb.append("Building Name: " + buildingName + '\n');
		sb.append("Delviery Instruction: " + deliveryInstruction + '\n');
		sb.append("Delivery Status: " + deliveryStatus + "\n\n");
		return sb.toString();
	}
	
	/* END (Object) Methods */
	
}
